package softtech.apps.autoanswerphonecall;

import java.lang.reflect.Method;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;

public class PhoneListenerService extends Service {
	private MediaRecorder recorder;
	private boolean recording = false;
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		Log.v("TAG", "service onCreate()");
		super.onCreate();
		TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		manager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
	}

	private PhoneStateListener listener = new PhoneStateListener() {
		/**
		 * @see TelephonyManager#CALL_STATE_IDLE 0
		 * @see TelephonyManager#CALL_STATE_RINGING 1
		 * @see TelephonyManager#CALL_STATE_OFFHOOK 2
		 */
		@Override
		public void onCallStateChanged(int state, String incomingNumber) {
			super.onCallStateChanged(state, incomingNumber);
			//
			Log.v("TAG", "onCallStateChanged state=" + state);
			switch (state) {
			case TelephonyManager.CALL_STATE_IDLE: //
				stopRecord();
				break;
			case TelephonyManager.CALL_STATE_RINGING: //
				stop(incomingNumber);
				break;
			case TelephonyManager.CALL_STATE_OFFHOOK: //
				recordCalling();
				break;
			default:
				break;
			}
		}
	};

	//
	private void stopRecord() {
		Log.v("TAG", "stopRecord");
		if (recording) {
			recorder.stop();
			recorder.release();
			recording = false;
		}
	}

	//
	public void stop(String s) {
		try {
			if (s.equals("110")) {
				Toast.makeText(this, "", 0).show();
				Log.e("TAG", "??");
				// ITelephony.endCall()
				Method method = Class.forName("android.os.ServiceManager")
						.getMethod("getService", String.class);
				IBinder binder = (IBinder) method.invoke(null,
						new Object[] { TELEPHONY_SERVICE });
//				ITelephony telephony = ITelephony.Stub.asInterface(binder);
//				telephony.endCall();
			} else
				Toast.makeText(this, "", 0).show();
			recording = false;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//
	private void recordCalling() {
		try {
			Log.v("TAG", "recordCalling");
			recorder = new MediaRecorder();
			recorder.setAudioSource(MediaRecorder.AudioSource.MIC); //
			recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);// .3gp
			recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//
			recorder.setOutputFile(Environment.getExternalStorageDirectory()
					.getAbsolutePath()
					+ "/"
					+ System.currentTimeMillis()
					+ ".3gp");// sdcard109 recorder.prepare();
			recorder.start();
			recording = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
